package com.hospital.diagnosis.module.service.impl;

import com.hospital.common.dto.diagnosis.module.ChatEvaluationDTO;
import com.hospital.common.pojo.TChatEvaluation;
import com.hospital.common.pojo.TChatSession;
import com.hospital.diagnosis.module.exception.ServiceException;
import com.hospital.diagnosis.module.mapper.ChatEvaluationMapper;
import com.hospital.diagnosis.module.service.ChatEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.hospital.diagnosis.module.exception.enums.ExceptionCode.IMPORT_RECORD_FAILED;
import static com.hospital.diagnosis.module.exception.enums.ExceptionCode.RECORD_NOT_FOUND;

@Service
public class ChatEvaluationServiceImpl implements ChatEvaluationService
{
    @Autowired
    private ChatEvaluationMapper chatEvaluationMapper;
    @Value("${evaluation.response-speed-weight}")
    private Double responseSpeedWeight;
    @Value("${evaluation.communication-weight}")
    private Double communicationWeight;
    @Value("${evaluation.diagnosis-accuracy-weight}")
    private Double diagnosisAccuracyWeight;

    @Override
    @Transactional
    public Boolean addChatEvaluation(Long patientId, ChatEvaluationDTO chatEvaluation)
    {
        List<TChatSession> chatSession = chatEvaluationMapper.getChatSessionBySessionId(chatEvaluation.getSessionId());
        if(ObjectUtils.isEmpty(chatSession)){
            throw new ServiceException(RECORD_NOT_FOUND);
        }
        if(!chatSession.get(0).getPatientId().equals(patientId)){
            throw new ServiceException(RECORD_NOT_FOUND);
        }
        if(chatSession.get(0).getChatEvaluationId()!=null){
            throw new ServiceException("会话已评价");
        }

        Double overallScoreRatio = chatEvaluation.getResponseSpeedScore()*responseSpeedWeight/5.0+
                chatEvaluation.getCommunicationScore()*communicationWeight/5.0+
                chatEvaluation.getDiagnosisAccuracyScore()*diagnosisAccuracyWeight/5.0;

        TChatEvaluation tChatEvaluation = new TChatEvaluation(
                null,
                chatEvaluation.getSessionId(),
                null,
                overallScoreRatio,
                chatEvaluation.getResponseSpeedScore(),
                chatEvaluation.getCommunicationScore(),
                chatEvaluation.getDiagnosisAccuracyScore(),
                chatEvaluation.getCommentText(),
                chatEvaluation.getIsComplaint()
        );

        if(chatEvaluationMapper.addChatEvaluation(tChatEvaluation)==0){
            throw new ServiceException(IMPORT_RECORD_FAILED,"评价添加失败");
        }
        if(chatEvaluationMapper.updateChatSession(chatSession.get(0).getId(),tChatEvaluation.getId())==0){
            throw new ServiceException(IMPORT_RECORD_FAILED,"会话评价状态更新失败");
        }

        return true;
    }
}
